import Vue from 'vue'
import Vuex from 'vuex'
import {getCartList} from "../api/index"
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    cartList:[],
  },
  getters: {
    cartNum(state) {
      return state.cartList.reduce((prev, cur) => {
        return prev + cur.goodsCount
      }, 0)
    }
  },
  mutations: {
    changeCartList(state,list){
      state.cartList=list.map(item=>{
        item.checked=true;
        return item
      })
      sessionStorage.setItem("cartList",JSON.stringify(state.cartList))
    }
  },
  actions: {
    changeCartListAsync(store) {   
      getCartList().then(data => {
        store.commit('changeCartList', data.data || [])
      })
    }
  },
  modules: {
  }
})
